﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormProject.Diger
{
    public partial class IlcelerFrm : Form
    {
        public IlcelerFrm()
        {
            InitializeComponent();
        }
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        public BindingSource MyNewSourceIlce = null;
        public BindingSource MyNewSourceIl = null;
        public DataSet MyDataSetIlce = new DataSet();
        public DataSet MyDataSetIl = new DataSet();

        int SecilenSehir = 34;

        public DataTable IlceGetir()
        {
            var sorgu = (from x in db.Ilcelers join y in db.Sehirlers on x.SehirID equals y.SehirID where x.SehirID == SecilenSehir select new { x.IlceID, x.IlceAdi, x.Sehirler.SehirAdi });
            SqlCommand cmd = db.GetCommand(sorgu) as SqlCommand;
            DataTable dt = new DataTable("Ilceler");
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            return dt;

        }

        public DataTable IlGetir()
        {
            var sorgu = (from x in db.Sehirlers select x);
            SqlCommand cmd = db.GetCommand(sorgu) as SqlCommand;
            DataTable dt = new DataTable("Sehirler");
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            return dt;

        }

        private void IlceDoldur()
        {
            if (MyDataSetIlce.Tables["Ilceler"] != null)
            {
                MyDataSetIlce.Tables.Remove("Ilceler");
            }
            MyDataSetIlce.Tables.Add(IlceGetir());
            MyNewSourceIlce = new BindingSource();
            MyNewSourceIlce.DataSource = MyDataSetIlce;
            MyNewSourceIlce.DataMember = MyDataSetIlce.Tables["Ilceler"].TableName;
            DGWIlceler.DataSource = MyNewSourceIlce;
            MyNewSourceIlce.Position = MyNewSourceIlce.Count;
            bindingNavigator1.BindingSource = MyNewSourceIlce;
            MyNewSourceIlce.PositionChanged += new EventHandler(MyNewSource_PositionChanged);
        }

        void MyNewSource_PositionChanged(object sender, EventArgs e)
        {
            IlceAdi.DataBindings.Clear();
            SehirID.DataBindings.Clear();
            IlceAdi.DataBindings.Add("Text", MyNewSourceIlce, "IlceAdi");
            SehirID.DataBindings.Add("Text", MyNewSourceIlce, "SehirAdi");
        }
        private void IlDoldur()
        {

            if (MyDataSetIl.Tables["Sehirler"] != null)
            {
                MyDataSetIl.Tables.Remove("Sehirler");
            }
            MyDataSetIl.Tables.Add(IlGetir());
            MyNewSourceIl = new BindingSource();
            MyNewSourceIl.DataSource = MyDataSetIl;
            MyNewSourceIl.DataMember = MyDataSetIl.Tables["Sehirler"].TableName;
            SehirID.DataSource = MyNewSourceIl;
            SehirID.DisplayMember = "SehirAdi";
            SehirID.ValueMember = "SehirID";
            MyNewSourceIl.Position = MyNewSourceIl.Count - 1;
            bindingNavigator1.BindingSource = MyNewSourceIl;
            SehirID.SelectedValue = 34;

        }

        private void IlcelerFrm_Load(object sender, EventArgs e)
        {
            IlDoldur();
            IlceDoldur();
        }

        private void bt_kaydet_Click(object sender, EventArgs e)
        {
            Ilceler Ilce = new Ilceler();
            var kayitkontrol = (from x in db.Ilcelers where x.IlceAdi == IlceAdi.Text select x).FirstOrDefault();
            if (kayitkontrol != null)
            {
                kayitkontrol.SehirID = Convert.ToInt32(SehirID.SelectedValue);
                db.SubmitChanges();
                IlceDoldur();
                MessageBox.Show("Kayıt Güncellendi");

            }
            else
            {
                Ilce.IlceAdi = IlceAdi.Text;
                Ilce.SehirID = Convert.ToInt32(SehirID.SelectedValue);
                db.Ilcelers.InsertOnSubmit(Ilce);
                db.SubmitChanges();
                IlceAdi.Text = string.Empty;
                MessageBox.Show("Kayıt tamamlandı");
                IlceDoldur();
            }
        }

        private void IlceAdi_ara_TextChanged(object sender, EventArgs e)
        {
            if (IlceAdi_ara.Text.Trim().Length > 0)
            {
                string filtre = "IlceAdi like '%" + IlceAdi_ara.Text + "%'";
                MyNewSourceIlce.Filter = filtre;
                MyNewSourceIlce.Position = MyNewSourceIlce.Count - 1;
            }
            else
            {
                MyNewSourceIlce.RemoveFilter();
                MyNewSourceIlce.Position = MyNewSourceIlce.Count - 1;
            }
        }

        private void bt_yeni_Click(object sender, EventArgs e)
        {
            if (MyNewSourceIlce.Filter != null)
            {
                MyNewSourceIlce.RemoveFilter();
            }
            MyNewSourceIlce.AddNew();
            IlceAdi.Text = string.Empty;
            IlceDoldur();
            IlDoldur();
        }

        private void SehirID_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                SecilenSehir = Convert.ToInt32(((DataRowView)SehirID.SelectedItem).Row["SehirID"]);
                IlceDoldur();
            }
            catch (Exception)
            {


            }


        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
        //    if (MessageBox.Show("Kaydı silmek için eminmisiniz?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
        //        MyNewSourceIlce.RemoveCurrent();
        }

        private void bt_sil_Click(object sender, EventArgs e)
        {
            var kayitkontrol = (from x in db.Ilcelers where x.IlceAdi == IlceAdi.Text select x).FirstOrDefault();
            if (kayitkontrol != null)
            {
                if (MessageBox.Show( "!"+ IlceAdi.Text +"\nKodlu ilçeyi silmek için emin misiniz?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                db.Ilcelers.DeleteOnSubmit(kayitkontrol);
                db.SubmitChanges();
                IlceDoldur();
            }
        }



       

      
       



    }
}
